# Social Mobilisation in Partisan Spaces
# Replication Materials
# Florian Foos, Peter John, Christian Mueller, and Kevin Cunningham

# Recoding and generation of variables
# last change: 2020-04-09

library("tidyverse")

# Load Data
d_raw <- read_rds("data_raw.rds")
d <- d_raw

# Helper function to easily mark observed (not NA) cases
is_observed <- function(x) !is.na(x)


# experiment_sample --------------------------------------------------

# Combine the three restrictions at the individual level (no_contact, postal,
# partisanship) to create a descriptive variable of whether the person would
# have been in the experiment ("direct") or excluded from the experiment
# ("indirect").

d <- d %>%
  mutate(
    experiment_sample_individual = factor(
      !(no_contact | postal | partisanship),
      levels = c(TRUE, FALSE),
      labels = c("direct", "indirect")
    )
  )

# Labour selected one household member as the recipient of the leaflet.
# Therefore, if at least one member in the household was treated, the whole
# household was (even those members who would have been excluded from the
# experiment as individuals). Thus, we expand the individual-level variable
# created above to generate the variable which denotes whether an individual
# was exposed to treatment ("direct") or not ("indirect").

d <- d %>%
  group_by(addr_hh_id) %>%
  mutate(
    experiment_sample = factor(any(experiment_sample_individual == "direct"), levels = c(TRUE, FALSE), labels = c("direct", "indirect"))
  ) %>%
  ungroup()


# Recode logical variables to 0/1
d <- d %>%
  mutate_at(
    c("no_contact", "postal", "partisanship"),
    as.integer
  )

# Create additional variables for the analysis
d <- d %>%
  mutate(
    treated_direct = as.integer(experiment_sample == "direct")
  )


# Combine treatment arms ---------------------------------------------

d <- d %>%
  mutate(
    treatment_assign = recode(
      treatment_assign_3cat,
      nhs = "treated",
      crime = "treated"
    )
  )

d <- d %>%
  mutate(
    treatment_deliver = recode(
      treatment_deliver_3cat,
      nhs = "treated",
      crime = "treated"
    )
  )


# marked_register_2014/2013 ------------------------------------------

d <- d %>%
  mutate(
    marked_register_2014 = as.numeric(marked_register_2014_raw == "Voted"),
    marked_register_2013 = as.numeric(marked_register_2013_raw == "Voted"),
  ) %>%
  mutate(
    marked_register_2014_nomiss = recode(marked_register_2014, .missing = 0),
    marked_register_2013_nomiss = recode(marked_register_2013, .missing = 0)
  )


# latest_voter_id_xxx ------------------------------------------------

d <- d %>%
  mutate(
    latest_voter_id_observed = as.integer(is_observed(latest_voter_id))
  ) %>%
  mutate(
    latest_voter_id_labour = as.integer(recode(latest_voter_id, "labour" = 1, .default = 0, .missing = 0))
  ) %>%
  mutate(
    latest_voter_id_conservative = as.integer(latest_voter_id %in% "conservative")
  ) %>%
  mutate(
    latest_voter_id_rivalparty = as.integer(latest_voter_id %in% c(
      "bnp", "green", "liberal democrat", "plaid cymru", "respect/soc lab",
      "snp", "uk independence", "against", "other party"))
  ) %>%
  mutate(
    latest_voter_id_nonvoter = as.integer(latest_voter_id %in% c("non voter"))
  ) %>%
  mutate(
    latest_voter_id_other = as.integer(latest_voter_id %in% c("don't know", "independent", "won't say"))
  )

# Create additional variables for the analysis
d <- d %>%
  mutate(
    partisan = latest_voter_id_conservative == 1 | latest_voter_id_labour == 1 |
      latest_voter_id_rivalparty == 1,
    rival = latest_voter_id_conservative == 1 | latest_voter_id_rivalparty == 1
  )


# street_rival_share -------------------------------------------------

# NB: Households which have more than 10 members do not contribute to the
# street rival share becausem these households are excluded from the analysis.

d <- left_join(
  x = d,
  y = d %>%
  filter(addr_hh_size <= 10) %>%
  group_by(ward, street) %>%
  summarise(
    street_rival_share =
      sum(latest_voter_id_conservative + latest_voter_id_rivalparty) / if_else(sum(latest_voter_id_observed) > 0, sum(latest_voter_id_observed), NA_integer_)
  ) %>%
  ungroup(),
  by = c("ward", "street")
)


# street_experiment_share --------------------------------------------

d <-
  d %>%
  group_by(ward, street) %>%
  mutate(
    street_experiment_share = mean(experiment_sample == "direct")
  ) %>%
  ungroup()


# street_turnout_share_2013 --------------------------------------------

d <-
  d %>%
  group_by(ward, street) %>%
  mutate(
    street_turnout_share_2013 = mean(marked_register_2013_nomiss)
  ) %>%
  ungroup()


# Write out ----------------------------------------------------------

write_rds(d, "data.rds", compress = "gz")
